package com.gitee.wsl.doc.prosemirror.model.node.ext

import com.gitee.wsl.doc.prosemirror.model.mark.MarkType
import com.gitee.wsl.doc.prosemirror.model.node.Node
import com.gitee.wsl.doc.prosemirror.state.selection.SelectionRange
import com.gitee.wsl.ext.base.ifValue


fun Node.markApplies( ranges: List<SelectionRange>, type: MarkType, enterAtoms: Boolean = false):Boolean {
    val doc = this
    ranges.forEach {
        val from = it.from
        val to = it.to
        var can = (from.depth == 0).ifValue( doc.inlineContent && doc.type.allowsMarkType(type) , false)
        doc.nodesBetween(from.pos, to.pos, f= {node, pos, parent, index ->
            if (can || !enterAtoms && node.isAtom && node.isInline && pos >= from.pos && pos + node.nodeSize <= to.pos)
                return@nodesBetween false
            can = node.inlineContent && node.type.allowsMarkType(type)
            true
        })
        if (can) return true
    }
    return false

//    for (let i = 0; i < ranges.length; i++) {
//        let {$from, $to} = ranges[i]
//        let can = $from.depth == 0 ? doc.inlineContent && doc.type.allowsMarkType(type) : false
//        doc.nodesBetween($from.pos, $to.pos, (node, pos) => {
//        if (can || !enterAtoms && node.isAtom && node.isInline && pos >= $from.pos && pos + node.nodeSize <= $to.pos)
//            return false
//        can = node.inlineContent && node.type.allowsMarkType(type)
//    })
//        if (can) return true
//    }
//    return false
}